package com.yaorange.module.system.service.impl;

import com.yaorange.exception.BusinessException;
import com.yaorange.exception.ValidateException;
import com.yaorange.module.system.repository.SysRolePermissionRepository;
import com.yaorange.module.system.repository.SysRoleRepository;
import com.yaorange.module.system.service.SysRolePermissionService;
import com.yaorange.module.system.service.SysRoleService;
import com.yaorange.module.system.service.SysUserRoleService;
import com.yaorange.pojo.SysRole;
import com.yaorange.response.enums.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;


@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleRepository sysRoleRepository;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Long id) {
        // 判断是否有用户关联了角色
        Long total = sysUserRoleService.getUserTotalByRoleId(id);
        if(total.longValue() > 0){
            // 有用户关联角色，不能删除
            throw new BusinessException(ResultCode.RELATION_ERROR);
        }
        // 删除角色
        sysRoleRepository.deleteById(id);
        // 删除角色权限关联信息
        sysRolePermissionService.deleteByRoleId(id);
    }

    @Override
    public void update(SysRole roleDetails) {
        if(roleDetails.getRoleId() == null){
            throw  new ValidateException("缺少参数，不能修改");
        }
        sysRoleRepository.update(roleDetails);
    }

    @Override
    public void save(SysRole user) {
        sysRoleRepository.insert(user);
    }

    @Override
    public SysRole findById(Long id) {
        return sysRoleRepository.selectById(id);
    }

    @Override
    public List<SysRole> findAllRoles() {
        return sysRoleRepository.selectList();
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        // 验证是否有用户关联角色
        if(CollectionUtils.isEmpty(ids)){
            throw new BusinessException(ResultCode.VALIDATE_FAILED);
        }
        boolean tag = false;
        for (int i = 0; i < ids.size(); i++) {
            Long id = ids.get(i);
            Long total = sysUserRoleService.getUserTotalByRoleId(id);
            if(total > 0){
                tag = true;
                throw new BusinessException(ResultCode.RELATION_ERROR);
            }
        }
        if(!tag){
            // 批量删除角色
            sysRoleRepository.deleteByIds(ids);
            // 批量删除角色的权限关联信息
            sysRolePermissionService.deleteByRoleIds(ids);
        }

    }

    @Override
    public List<Long> findPermissionIdsByRoleId(Long roleId) {
        return sysRoleRepository.selectPermissionIdsByRoleId(roleId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void grantPermissions(List<Long> permissionIds, Long roleId) {
        if(roleId == null){
            throw new ValidateException("缺少参数，不能操作");
        }
        // 删除角色关联的权限信息
        sysRolePermissionService.deleteByRoleId(roleId);
        // 批量插入角色权限关联信息
        sysRolePermissionService.insertBatch(permissionIds, roleId);
    }
}
